package gui;

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;

import com.ice.tar.TarArchive;
import com.ice.tar.TarEntry;

/**
 * @author suli
 *
 */
public class MapGenerator {
    private double longitude=49.23107583; // zeměpisná délka
    private double latitude=16.59193139;  // zeměpisná šířka 
    private int zoom = 16;
    private String maptype = "roadmap";
    
    private BufferedImage  cacheImage= null;
     
	public BufferedImage getMap(){
		if (cacheImage == null){
			URL url=null;
			try {
				url = new URL("http://maps.google.com/staticmap?size=940x940&format=png&center="+longitude+","+latitude+"&zoom="+zoom+"&maptype="+maptype+"&key=MAPS_API_KEY&sensor=true_or_false");
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
			try {
			//	throw new IOException();
				cacheImage = ImageIO.read(url);
			} catch (IOException e) {				
				System.err.println("Chyba při stahování obrázku používám lokální");
				try {
					cacheImage = ImageIO.read(new File("staticmap.png"));
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
		return  cacheImage;

	}
	
	public void generateMaps(String tarName, String mapName) throws IOException{
			
		int pieces = 5;
	
		new File(mapName).mkdir();
		
		BufferedWriter out = new BufferedWriter(new FileWriter(mapName+"/info.txt"));
		out.write(("N "+String.format("%011f",getMap().getWidth()*longitudePixelSize()/pieces)+" E "+String.format("%011f",getMap().getHeight()*latitudePixelSize()/pieces)+" R0S0 " +
				String.format("%011f",longitude+(getMap().getWidth()/2*longitudePixelSize())) +"N "+String.format("%011f",latitude-(getMap().getHeight()/2*latitudePixelSize()))+"E"+
				" RBC "+String.format("%012f",longitude-(getMap().getWidth()/2*longitudePixelSize())) +"N "+String.format("%012f",latitude+(getMap().getHeight()/2*latitudePixelSize()))+"E"+
				" tw " + String.format("%04d", getMap().getWidth()/pieces)+" th "+String.format("%04d", getMap().getHeight()/pieces)).replace(',', '.'));
		out.close();
		
		for (int h=0; h < pieces; h++)
			for (int w=0; w < pieces; w++){
				ImageIO.write((RenderedImage) getMap().getSubimage(h*getMap().getHeight()/pieces, w*getMap().getWidth()/pieces, getMap().getHeight()/pieces, getMap().getWidth()/pieces),"png", new File(mapName+"/R"+w+"S"+h+".png"));
			}
		
		File tarFile = new File(tarName);
		FileOutputStream outTar = new FileOutputStream(tarFile);
		TarArchive tar = new TarArchive(outTar);
		for (File file : new File(mapName).listFiles()) {
			TarEntry tarEntry = new TarEntry(file);
			tar.writeEntry(tarEntry, false);
		}
		tar.closeArchive();
		outTar.close();
		
		for (File file : new File(mapName).listFiles()) 
			file.delete();
		new File(mapName).delete();
	}
    
	
	public double longitudePixelSize() {
		return (240/Math.pow(2.0,(double)getZoom()))/256;
	}

	public double latitudePixelSize() {
		return (360/Math.pow(2.0,(double)getZoom()))/256;
	}
	
	public void setLongitude(double longitude) {
		this.longitude = longitude;
		cacheImage = null;
	}
	public void setLatitude(double latitude) {
		this.latitude = latitude;
		cacheImage = null;
	}

	public void setZoom(int zoom) {
		this.zoom = zoom;
		cacheImage = null;
	}

	public double getLongitude() {
		return longitude;
	}

	public double getLatitude() {
		return latitude;
	}

	public int getZoom() {
		return zoom;
	}

	public String getMaptype() {
		return maptype;
	}

	public void setMaptype(String maptype) {
		this.maptype = maptype;
		cacheImage = null;
	}
	    
	

 
    
}
